package com.goods.controller.business;

import com.goods.business.service.ProductCategoryService;
import com.goods.common.annotation.ControllerEndpoint;
import com.goods.common.error.BusinessException;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;

import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "业务模块-物资类别相关接口")
@RestController
@RequestMapping("/business/productCategory")
public class ProductCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;

    //加载分类数据
    @GetMapping("/categoryTree")
    public ResponseBean categoryTree(@RequestParam(value = "pageNum", required = false) Integer pageNum,
                                     @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        PageVO<ProductCategoryTreeNodeVO> pageVO =
                productCategoryService.categoryTree(pageNum, pageSize);
        return ResponseBean.success(pageVO);
    }

    //获取父分类数据
    @GetMapping("/getParentCategoryTree")
    public ResponseBean getParentCategoryTree() {
        List<ProductCategoryTreeNodeVO> PidList =
                productCategoryService.getParentCategoryTree();
        return ResponseBean.success(PidList);
    }

    //添加
    @PostMapping("/add")
    public ResponseBean add(@RequestBody @Validated ProductCategoryVO productCategoryVO) {
        productCategoryService.add(productCategoryVO);
        return ResponseBean.success();
    }

    //编辑 先根据ID进行数据回显
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id) {
        ProductCategoryVO productCategoryVO =
                productCategoryService.edit(id);
        return ResponseBean.success(productCategoryVO);
    }

    //编辑 修改
    @PutMapping("/update/{id}")
    public ResponseBean update(@PathVariable Long id,
                               @RequestBody @Validated ProductCategoryVO productCategoryVO){
        productCategoryService.update(id , productCategoryVO);
        return ResponseBean.success();
    }

    //删除
    @ControllerEndpoint(exceptionMessage = "物资分类删除失败", operation = "物资分类删除")
    @ApiOperation(value = "删除分类")
    @RequiresPermissions({"productCategory:delete"})
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id)throws BusinessException {
        productCategoryService.delete(id);
        return ResponseBean.success();
    }
}
